//
//  NSString+HtmlFilter.m
//  YiDian
//
//  Created by majia on 2018/7/4.
//  Copyright © 2018年 Jeagine. All rights reserved.
//

#import "NSString+HtmlFilter.h"

@implementation NSString (HtmlFilter)


/**
 * 过滤html标签
 */
+(NSString *)filterHTML:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return @"";
    }
    
    NSScanner * scanner = [NSScanner scannerWithString:str];
    NSString * text = nil;
    while([scanner isAtEnd] == NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        str  =  [str  stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    return str;
}
/**
 * 过滤图片html标签
 */
+(NSString *)filterHTMLImage:(NSString *)str{
    NSScanner * scanner = [NSScanner scannerWithString:str];
    NSString * text = nil;
    while([scanner isAtEnd] == NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<img" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        str  =  [str  stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    return str;
}

/** 过滤某字符 */
+ (NSString*) filterKeyword:(NSString*)keyword inString:(NSString*)str {
    if(!keyword) {
        return str;
    }
    NSString *resultString = [str stringByReplacingOccurrencesOfString:keyword withString:@""];
    return resultString;
}

@end
